package com.work.demo.file;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.util.StrUtil;

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author admin
 */
public class TraceTest {

	volatile int i = 0;

	public void test() throws InterruptedException {
		ExecutorService executor = ExecutorBuilder.create()
						.setCorePoolSize(5)
						.setMaxPoolSize(10)
						.setWorkQueue(new LinkedBlockingQueue<>(100))
						.build();
		ExecutorService executor2 = ExecutorBuilder.create()
						.setCorePoolSize(1)
						.setMaxPoolSize(1)
						.setWorkQueue(new LinkedBlockingQueue<>(100))
						.build();
		final long start = System.currentTimeMillis();

		executor.submit(() -> {
			long id = Thread.currentThread().getId();
			Thread.currentThread().setName("saveLocalFile" + id);
			String name = Thread.currentThread().getName();
			try {
				String today = DateUtil.today();
				String[] split = StrUtil.split(today, "-").toArray(new String[0]);
				String year = split[0];
				String month = split[1];

				File file = BaseFileUtil.createCsvFile("D:\\" + year + "" + month + "\\" + name + "\\LinYa1.txt");

				OutputStream os = new FileOutputStream(file);
				OutputStreamWriter writer = new OutputStreamWriter(os);
				writer.write(name + "\n");
				writer.close();
				executor2.submit(() -> {
					//调用上传fastDFS接口
					//如果抛出非定义的异常，则重新上传文件

					//如果是定义好的异常，则正常的抛出异常
				});
			} catch (IOException e) {
				e.printStackTrace();
			}
		});

		executor.shutdown();


		while (true) {
			if (executor.isTerminated()) {
				System.out.println("所有的子线程都结束了！");
				break;
			}
//            Thread.sleep(1000);
		}

		final long end = System.currentTimeMillis();
		System.out.println((end - start) / 1000);
	}

	public static void main(String[] args) throws InterruptedException {
		TraceTest traceTest = new TraceTest();
		for (int i = 0; i < 10; i++) {
			traceTest.test();
		}
	}
}
